package com.lab.book.seata.service;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;

import com.lab.book.common.bean.User;
import com.lab.book.common.service.UserService;
import com.lab.book.seata.common.bean.Address;
import com.lab.book.seata.mapper.AddressMapper;

import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;

//import io.seata.spring.annotation.GlobalTransactional;
@Slf4j
@org.springframework.stereotype.Service
public class AddressServiceImpl implements AdressService {
	@Autowired
	private AddressMapper addressMapper;

	@Reference(timeout = 4000999)
	private UserService userService;

	@Override
	public Address addAddress(Address address) throws Exception {
		addressMapper.add(address);
		return address;
	}

	@Override
	@GlobalTransactional(name = "abc-seata-test", rollbackFor = Exception.class)
	public String seataTest(User user, Address address) throws Exception {
		try {
			user = userService.addUser(user);
			log.info("The userId is ........... " + user.getId());
			
			address.setUserId(user.getId());
			address = addAddress(address);
			
			log.info("The addressId is ........... " + address.getId());
			
			int a = 2/0;
			return "Save Ok...111";
		} catch (Exception e) {
			throw new RuntimeException("RollBack Transaction...2/0 .Seata..." + e.getMessage());
		}

	}

}
